/**
 * spring board会话列表. 
 */
var chatGroupList = (function(){   

    return Observable.create({
        init : function(){
            this.cardList = new CardList($('#chatGroupList'), CardListView.chatCardTmp);
            this.cardList.$itemAdder.attr('title', lan('app.im.chat.createChat'));    
            this.cardList.$itemAdder.click(function(){
                ujs.ui.Dialog.get( ChatGroupDialog, {
                    title : lan('app.im.chat.newChat')
                }); 
            });        
            //单击chat icon进入会话.
            this.cardList.on('cardIconClick', function(cardID){
                sessionList.startSession(cardID);
            });           
            //delete会话, 退出会话
            this.cardList.on('deleteItem', function(cardID){
                var _this = this;
                var chatGroupEntity = page.chatGroupStore.getItem(cardID);
                var confirmText = lan('app.im.chat.delTip');
                if(chatGroupEntity.perm && chatGroupEntity.perm.del == '3'){
                    confirmText = lan('app.im.chat.delAdminChatTip');
                }
                bubble.confirm(confirmText.format(chatGroupEntity['name']), function(){
                    page.biz.exitChatGroup(cardID, function(msg){
                        _this.deleteItem(cardID);
                        sessionList.deleteSession(cardID);
                    }); 
                });             
            });
            //编辑chat.
            this.cardList.on('editItem', function(cardID){                
                ujs.ui.Dialog.get(ChatGroupDialog, {
                    title : lan('app.im.chat.editChat'),
                    mode : 'edit',
                    teid : cardID
                }); 
            });
            this.cardList.$itemAdder.tooltipster({
                theme : 'tooltipster-light',
                offsetY : -70
            });
        }, 
        updateLastMsg : function(topic){            
            //更新最后消息.
            var $lastMsgWrapper = this.cardList.$wrapper.find('.cardWrapper[card-id={0}]'.format(topic['teid'])).find('.cardLastMsg');         
            $lastMsgWrapper.text(ChatMsg.getMsgDesp(topic));
        }
    });
})();